﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace com.zhongjh.Library.Common
{
    /// <summary>
    /// 错误处理类
    /// </summary>
    public static class Error
    {
        #region 字段

        private static Stack<ErrorEntity> m_objErrorList = new Stack<ErrorEntity>();

        #endregion //字段

        #region 公有方法


        /// <summary>
        /// 添加错误
        /// </summary>
        /// <param name="p_intErrorId"></param>
        /// <param name="p_strUserErrorMsg"></param>
        /// <param name="p_strSysErrorMsg"></param>
        public static void AppendError(int p_intErrorId, string p_strUserErrorMsg, string p_strSysErrorMsg)
        {
            string strErrorSource = string.Empty;
            StackTrace stStackTrace = new StackTrace();
            StackFrame sfStackFrame = stStackTrace.GetFrame(1);
            MethodBase mbMethodBase = sfStackFrame.GetMethod();
            string errorSource = " Assembly: " + mbMethodBase.Module + "\n Namespace: " + mbMethodBase.DeclaringType + "\n Mathod: " + mbMethodBase.Name;

            ErrorEntity error = new ErrorEntity();
            error.Id = m_objErrorList.Count + 1;
            error.ErrorId = p_intErrorId;
            error.ErrorSource = errorSource;
            error.UserErrorMsg = p_strUserErrorMsg;
            error.SysErrorMsy = p_strSysErrorMsg;
            m_objErrorList.Push(error);
        }

        /// <summary>
        /// 添加异常
        /// </summary>
        /// <param name="p_objException"></param>
        /// <param name="p_strUserErrorMsg"></param>
        public static void AppendException(Exception p_objException, string p_strUserErrorMsg)
        {
            if (p_objException == null)
                return;

            string strErrorSource = string.Empty;
            StackTrace stStackTrace = new StackTrace(p_objException);
            StackFrame sfStackFrame = stStackTrace.GetFrame(1);
            MethodBase mbMethodBase = sfStackFrame.GetMethod();
            string errorSource = " Assembly: " + mbMethodBase.Module + "\n Namespace: " + mbMethodBase.DeclaringType + "\n Mathod: " + mbMethodBase.Name;

            ErrorEntity error = new ErrorEntity();
            error.Id = m_objErrorList.Count + 1;
            error.ErrorId = 0;
            error.ErrorSource = errorSource;
            error.UserErrorMsg = p_strUserErrorMsg;
            error.SysErrorMsy = p_objException.Message + "\r\n" + p_objException.StackTrace;
            m_objErrorList.Push(error);
        }



        /// <summary>
        /// 添加异常
        /// </summary>
        /// <param name="p_objException"></param>
        public static void AppendException(Exception p_objException)
        {
            AppendException(p_objException, string.Empty);
        }

        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="p_strOutputPath"></param>
        public static void WriteErrorLog(string p_strOutputPath)
        {
            Log objLog = new Log(p_strOutputPath);
            ErrorEntity objErrorEntity = null;
            for (int i1 = 0; i1 < m_objErrorList.Count; i1++)
            {
                objErrorEntity = m_objErrorList.Pop();
                objLog.WriteError(objErrorEntity);
            }
        }

        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="p_strOutputPath"></param>
        public static void WriteErrorLog()
        {
            string strPath = AppDomain.CurrentDomain.BaseDirectory + string.Format(@"log\{0}.log", DateTime.Now.ToString("yyyyMMddHHmmss"));
            if (Folder.CreateFolderPath(strPath))
                WriteErrorLog(strPath);
        }

        #endregion //公有方法
    }
}
